package com.fengr.org.service.impl;

import com.fengr.basic.service.impl.BasicServiceImpl;
import com.fengr.org.domain.Department;
import com.fengr.org.mapper.DepartmentMapper;
import com.fengr.org.query.QueryDept;
import com.fengr.org.service.IDepartmentService;
import com.fengr.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DepartmentServiceImpl extends BasicServiceImpl<Department>implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void insert(Department department) {
        departmentMapper.insert(department);
        String dirPath = "";
        if (department.getParent() == null) {
            dirPath = "/" + department.getId();
        } else {
            Department parentDept = departmentMapper.findOne(department.getParent().getId());
            dirPath = parentDept.getDirPath() + "/" + department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    @Override
    public void update(Department department) {
        String dirPath = "";

        if (department.getParent().getId()==null) {
            dirPath = "/" + department.getId();
        } else {
            Department parentDept = departmentMapper.findOne(department.getParent().getId());
            dirPath = parentDept.getDirPath() + "/" + department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }
    @Override
    public void patchDelete(List<Long> ids) {
        departmentMapper.patchDelete(ids);
    }
    @Override
    public List<Department> getDeptTree() {
        return departmentMapper.getDeptTree();
    }
}